package com.csap.framework.oauth2.security.user;

import com.csap.framework.oauth2.UserInfo;

/**
 * @Author ycf
 * @Date 2021/8/24 7:11 下午
 * @Version 1.0
 */
public class UserContext {
    private UserContext() {
    }

    private static final ThreadLocal<UserInfo> CONTEXT = ThreadLocal.withInitial(() -> null);

    /**
     * 存放用户信息
     *
     * @param userInfo
     */
    public static void set(UserInfo userInfo) {
        CONTEXT.set(userInfo);
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    public static UserInfo get() {
        return CONTEXT.get();
    }

    /**
     * 清除当前线程内引用，防止内存泄漏
     */
    public static void remove() {
        CONTEXT.remove();
    }
}
